<%= form_with(model: false, url: mass_update_jobs_path(filter.to_params), method: :put, local: true, data: { "checkbox-toggle": "job_ids" }) do |form| %>
  <div class="my-3 card" data-gj-poll-replace id="jobs-table">
    <div class="list-group list-group-flush text-nowrap table-jobs" role="table">
      <header class="list-group-item bg-body-tertiary">
        <div class="row small text-muted text-uppercase align-items-center">
          <div class="col-lg-4 d-flex gap-2 flex-wrap">
            <div class="form-check d-flex flex-row px-0 mb-0">
              <%= check_box_tag('toggle_job_ids', "1", false, data: { "checkbox-toggle-all": "job_ids" }) %>
              <%= label_tag('toggle_job_ids', t(".toggle_all_jobs"), class: "visually-hidden") %>
            </div>
            <%= form.button type: 'submit', name: 'mass_action', value: 'reschedule', class: 'ms-1 btn btn-sm btn-outline-secondary', title: t(".actions.reschedule_all"), data: { confirm: t(".actions.confirm_reschedule_all"), disable: true } do %>
              <span class="me-1"><%= render_icon "skip_forward" %></span> <%= t "good_job.actions.reschedule" %>
            <% end %>

            <%= form.button type: 'submit', name: 'mass_action', value: 'retry', class: 'btn btn-sm btn-outline-secondary', title: t(".actions.retry_all"), data: { confirm: t(".actions.confirm_retry_all"), disable: true } do %>
              <span class="me-1"><%= render_icon "arrow_clockwise" %></span> <%= t "good_job.actions.retry" %>
            <% end %>

            <div class="btn-group" role="group">
              <%= form.button type: 'submit', name: 'mass_action', value: 'discard', class: 'btn btn-sm btn-outline-secondary', title: t(".actions.discard_all"), data: { confirm: t(".actions.confirm_discard_all"), disable: true } do %>
                <span class="me-1"><%= render_icon "stop" %></span> <%= t "good_job.actions.discard" %>
              <% end %>
              <button id="destroy-dropdown-toggle" type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
                <span class="visually-hidden"><%= t ".toggle_actions" %></span>
              </button>
              <ul class="dropdown-menu" aria-labelledby="destroy-dropdown-toggle">
                <li>
                  <%= form.button type: 'submit', name: 'mass_action', value: 'destroy', class: 'btn dropdown-item', title: t(".actions.destroy_all"), data: { confirm: t(".actions.confirm_destroy_all"), disable: true } do %>
                    <span class="me-1"><%= render_icon "trash" %></span> <%= t "good_job.actions.destroy" %>
                  <% end %>
                </li>
              </ul>
            </div>

          </div>
          <div class="d-none d-lg-block col-lg-1 text-lg-center"><%= t "good_job.models.job.queue" %></div>
          <div class="d-none d-lg-block col-lg-1 text-lg-end"><%= t "good_job.models.job.priority" %></div>
          <div class="d-none d-lg-block col-lg-1 text-lg-end"><%= t "good_job.models.job.labels" %></div>
          <div class="d-none d-lg-block col-lg-1 text-lg-end"><%= t "good_job.models.job.attempts" %></div>
          <div class="col text-end">
            <%= tag.button type: "button", class: "btn btn-sm text-muted", role: "button",
              data: { bs_toggle: "collapse", bs_target: ".job-params" },
              aria: { expanded: false, controls: jobs.map { |job| "##{dom_id(job, 'params')}" }.join(" ") } do %>
              <%= render_icon "info" %>
              <span class="visually-hidden"><%= t "good_job.actions.inspect" %></span>
            <% end %>
          </div>
        </div>
      </header>
      <label role="row" class="list-group-item list-group-item-warning list-group-item-action py-2 d-none" data-checkbox-toggle-show="job_ids">
        <div class="row">
          <div class="col-auto">
            <%= check_box_tag "all_job_ids", 1, false, disabled: true, data: { "checkbox-toggle-show": "job_ids" } %>
          </div>
          <div class="col-auto">
            <%= t ".actions.apply_to_all", count: filter.filtered_count %>
          </div>
        </div>
      </label>

      <% if jobs.present? %>
        <% jobs.each do |job| %>
          <%= label_tag dom_id(job, :checkbox), id: dom_id(job), role: "row", class: "list-group-item list-group-item-action py-3" do %>
            <div class="row align-items-center">
              <div class="col-lg-4 d-flex">
                <%= check_box_tag 'job_ids[]', job.id, false, id: dom_id(job, :checkbox), data: { "checkbox-toggle-each": "job_ids" } %>
                <div class="ms-2">
                  <%= tag.code link_to(job.id, job_path(job), class: "small text-muted text-decoration-none") %>
                  <%= tag.h5 tag.code(link_to(job.display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %>
                  <% if job.error %>
                    <div class="mt-1 small">
                      <strong class="small text-danger"><%= t "good_job.shared.error" %>:</strong>
                      <code class="text-wrap text-break m-0 text-secondary-emphasis"><%= job.error %></code>
                    </div>
                  <% end %>
                </div>
              </div>
              <div class="col-4 col-lg-1 text-lg-center">
                <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.queue" %></div>
                <span class="badge text-bg-primary font-monospace"><%= job.queue_name %></span>
              </div>
              <div class="col-4 col-lg-1 text-lg-end">
                <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.priority" %></div>
                <%= job.priority %>
              </div>
              <div class="col-4 col-lg-1 text-wrap text-lg-end">
                <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.labels" %></div>
                <% job.labels&.each do |label| %>
                  <span class="badge rounded-pill text-bg-secondary font-monospace"><%= label %></span>
                <% end %>
              </div>
              <div class="col-4 col-lg-1 text-lg-end">
                <div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.job.attempts" %></div>
                <% if job.error %>
                  <%= tag.span job.executions_count, class: "badge rounded-pill text-bg-danger",
                    data: {
                      bs_toggle: "popover",
                      bs_trigger: "hover focus click",
                      bs_placement: "bottom",
                      bs_content: job.display_error,
                    }
                  %>
                <% else %>
                  <% executions_badge_color = job.executions_count > 1 ? "text-bg-warning" : "text-bg-secondary" %>
                  <span class="badge rounded-pill <%= executions_badge_color %>"><%= job.executions_count %></span>
                <% end %>
              </div>
              <div class="mt-3 mt-lg-0 col">
                <div class="d-flex gap-3 align-items-start justify-content-end">
                  <%= tag.span relative_time(job.last_status_at), class: "small mt-1" %>
                  <div>
                    <%= status_badge job.status %>
                    <% if job.status == :discarded && job.error_event %>
                      <div class="text-center">
                        <small><%= t(job.error_event, scope: 'good_job.error_event') %></small>
                      </div>
                    <% end %>
                  </div>

                  <div class="dropdown float-end">
                    <button class="d-flex align-items-center btn btn-sm" type="button" id="<%= dom_id(job, :actions) %>" data-bs-toggle="dropdown" aria-expanded="false">
                      <%= render_icon "dots" %>
                      <span class="visually-hidden"><%= t ".actions.title" %></span>
                    </button>
                    <ul class="dropdown-menu shadow" aria-labelledby="<%= dom_id(job, :actions) %>">
                      <li>
                        <% job_reschedulable = job.status.in? [:scheduled, :retried, :queued] %>
                        <%= link_to reschedule_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job_reschedulable}", title: t("good_job.jobs.actions.reschedule"), data: { confirm: t("good_job.jobs.actions.confirm_reschedule"), disable: true } do %>
                          <%= render_icon "skip_forward" %>
                          <%= t "good_job.actions.reschedule" %>
                        <% end %>
                      </li>
                      <li>
                        <% job_discardable = job.status.in? [:scheduled, :retried, :queued] %>
                        <%= link_to discard_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job_discardable}", title: t("good_job.jobs.actions.discard"), data: { confirm: t("good_job.jobs.actions.confirm_discard"), disable: true } do %>
                          <%= render_icon "stop" %>
                          <%= t "good_job.actions.discard" %>
                        <% end %>
                      </li>
                      <li>
                        <% job_force_discardable = job.status.in? [:running] %>
                        <%= link_to force_discard_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job_force_discardable}", title: t("good_job.jobs.actions.force_discard"), data: { confirm: t("good_job.jobs.actions.confirm_force_discard"), disable: true } do %>
                          <%= render_icon "eject" %>
                          <%= t "good_job.actions.force_discard" %>
                        <% end %>
                      </li>
                      <li>
                        <%= link_to retry_job_path(job.id), method: :put, class: "dropdown-item #{'disabled' unless job.status == :discarded}", title: t("good_job.jobs.actions.retry"), data: { confirm: t("good_job.jobs.actions.confirm_retry"), disable: true } do %>
                          <%= render_icon "arrow_clockwise" %>
                          <%= t "good_job.actions.retry" %>
                        <% end %>
                      </li>
                      <li>
                        <%= link_to job_path(job.id), method: :delete, class: "dropdown-item #{'disabled' unless job.status.in? [:discarded, :succeeded]}", title: t("good_job.jobs.actions.destroy"), data: { confirm: t("good_job.jobs.actions.confirm_destroy"), disable: true } do %>
                          <%= render_icon "trash" %>
                          <%= t "good_job.actions.destroy" %>
                        <% end %>
                      </li>

                      <li>
                        <%= link_to "##{dom_id(job, 'params')}",
                          class: "dropdown-item",
                          data: { bs_toggle: "collapse" },
                          aria: { expanded: false, controls: dom_id(job, "params") } do %>
                          <%= render_icon "info" %>
                          <%= t "good_job.actions.inspect" %>
                        <% end %>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          <% end %>
          <%= tag.div id: dom_id(job, "params"), class: "job-params list-group-item collapse small bg-dark text-light" do %>
            <%= tag.pre JSON.pretty_generate(job.display_serialized_params) %>
          <% end %>
        <% end %>
      <% else %>
        <div class="list-group-item py-4 text-center text-muted">
          <%= t ".no_jobs_found" %>
        </div>
      <% end %>
    </div>
  </div>
<% end %>
